package com.codez.gznx.excel;

/**
 * -- 获取excel  A_Z 对应的列索引
 * @author dechao.zheng
 * @createTime: 2019-09-24 03:50:10	
 *
 */

public class AzUtil {

	private static final String A_Z = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	
	public static void main(String[] args) {
		System.out.println(getIndex("A"));
		System.out.println(getStr(1));
		System.out.println(getStr(28*26+1));
		System.out.println(getExcelStr(25));
		System.out.println(getExcelStr(26));
	}
	
	/**
	 * --根据AZ获取到响应的索引 A=1 Z=26
	 * @param AzStr
	 * @return
	 */
	public static int getIndex(String AzStr) {
//		AzStr = AzStr.toUpperCase();
		int res = 0;
		for (int i = 0; i < AzStr.length(); i++) {
			int index = A_Z.indexOf(AzStr.charAt(i));
			res += Math.pow(26, AzStr.length() - i - 1) * index;
		}
		return res;
	}
	
	/**
	 * 根据索引得到字符串
	 * @param value
	 * @return
	 */
	public static String getStr(int value) {
		String res ="";
		do {
			int index = 0;
			if(value == 26) {
				index = 26;
				value = 0;
			}else {
				index = value % 26;
				value = value / 26;
			}
			res = A_Z.charAt(index) + res;
		}while(value != 0);
		
		return res;
	}
	
	public static String getExcelStr(int value) {
		// 因为value是从开始,所以加1
		return getStr(value + 1);
	}
}
